﻿@*
    For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
*@
@{
    Layout = null;
}
@* 后裔转种添加 *@
<link href="~/lib/layui/css/layui.css" rel="stylesheet" />
<script src="~/lib/layui/layui.js"></script>
<script src="~/js/js.cookie.js"></script>
<script src="~/js/moment.js"></script>
<script src="~/lib/jquery/dist/jquery.js"></script>
<!DOCTYPE html>
<html>
@* <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Demo</title>
    <!-- 请勿在项目正式环境中引用该 layui.css 地址 -->
    <link href="//unpkg.com/layui@2.9.18/dist/css/layui.css" rel="stylesheet">
</head> *@
<body>
    
    <form class="layui-form" action="">
        <div class="layui-form-item">
            <div class="layui-form-item">


                <div class="layui-inline">

                    <label class="layui-form-label">转种日期</label>
                    <div class="layui-input-inline layui-input-wrap">
                        <input type="date" id="crossbreedingTime" lay-verify="date" autocomplete="off" class="layui-input">
                    </div>

                    <div class="layui-form-item">
                        <label class="layui-form-label">后裔耳号</label>
                        <select id="bredSheepNo">
                            <option value="">请选择</option>
                        </select>
                    </div>

                </div>
                <button type="button" class="layui-btn layui-bg-red" onclick="Add()" lay-on="test-iframe-curl">新增</button>

            </div>
        </div>


        <table class="layui-table">
            <thead>
                <tr>
                    <td>育成羊耳号</td>
                    <td>转入栋舍</td>
                    <td>转入栏位</td>
                    <td>当前栋舍</td>
                    <td>当前栏位</td>
                    <td>性别</td>
                    <td>转种日龄</td>

                </tr>
            </thead>
            <tbody>
                <tr>
                    <td>
                        <select id="earBugles" lay-filter="testSelect">
                            <option value="">请选择</option>
                        </select>
                    </td>
                    <td>
                        <select id="cottageId" lay-filter="provincet">
                            <option value="">请选择</option>
                        </select>
                    </td>
                    <td>
                        <select id="field" lay-filter="city">
                            <option value="">请选择</option>
                        </select>
                    </td>
                    <td> <input type="text" id="cottageName" class="layui-input"></td>
                    <td> <input type="text" id="fieldName" class="layui-input"></td>
                    <td><input type="text" id="sex" class="layui-input"></td>
                    <td><input type="text" id="ageTransfer" class="layui-input"></td>
                </tr>
            </tbody>
        </table>
    </form>


    <!-- 请勿在项目正式环境中引用该 layui.js 地址 -->
    <script src="//unpkg.com/layui@2.9.18/dist/layui.js"></script>
    <script>
        layui.use(['table'], function () {
             var table = layui.table;
             var form = layui.form;
             var layer = layui.layer;

         });

    </script>


</body>

</html>
<script>
    layui.use('form', function () {
        var table = layui.table;
        var form = layui.form;
        var layer = layui.layer;

        //绑定耳号
        bangsheng1();
        bangsheng2();
        //绑定栋舍
        bangsheng();

        FantearBugles();



        //获取耳号下拉框的值
        form.on('select(testSelect)', function (data) {
            FantearBugles(data.value); // data.value 返回选中的值
            console.log(data.value, 6666666666)
        });



        //绑定栋舍
        function bangsheng() {
            $.ajax({
                url: "http://localhost:5123/api/Cottage/CottageSecondaryQuery",
                data: JSON.stringify({}),
                type: "post",
                async: false,
                dataType: "json",
                contentType: "application/json",
                success: res => {
                    console.log(res)
                    $.each(res.data, function () {
                        $("#cottageId").append(`<option value="${this.cottageId}">${this.cottageName}</option>`);
                    })
                    form.render('select'); // 重新渲染下拉框
                }
            })
        }

        //获取栋舍下拉框的值
        form.on('select(provincet)', function (data) {
            provincetId1 = data.value;
            console.log(provincetId1, 15152)
            $("#field").empty();
            $("#field").append('<option value="">请选择</option>');
            bangshi()

        });

        //绑定栏位
        function bangshi() {
            var cottageId = $("#cottageId").val();
            $.ajax({
                url: "http://localhost:5123/api/Field/GetFieldCottage",
                data: JSON.stringify({ cottageId: cottageId }),
                type: "post",
                async: false,
                dataType: "json",
                contentType: "application/json",
                success: res => {
                    console.log(res.data)

                    $.each(res.data, function () {
                        $("#field").append(`<option value="${this.fieldId}">${this.fieldName}</option>`);
                    })
                    form.render('select'); // 重新渲染下拉框
                }

            })
        }

        //绑定耳号
        function bangsheng1() {

            $.ajax({
                url: "http://localhost:5269/api/SheepArchive/SheepEarBuglesQuery",
                data: JSON.stringify({}),
                type: "post",
                async: false,
                dataType: "json",
                contentType: "application/json",
                success: res => {
                    console.log(res)
                    $.each(res.data, function () {
                        $("#earBugles").append(`<option value="${this.recordAuricle}">${this.recordAuricle}</option>`);
                    })
                    form.render('select'); // 重新渲染下拉框

                }

            })
        }
        //后裔耳号
        function bangsheng2() {

            $.ajax({
                url: "http://localhost:5269/api/SheepArchive/SheepEarBuglesQuery",
                data: JSON.stringify({}),
                type: "post",
                async: false,
                dataType: "json",
                contentType: "application/json",
                success: res => {
                    console.log(res)
                    $.each(res.data, function () {
                        $("#bredSheepNo").append(`<option value="${this.recordId}">${this.recordAuricle}</option>`);
                    })
                    form.render('select'); // 重新渲染下拉框

                }

            })
        }

        //根据耳号加载数据
        function FantearBugles(aa) {
            //var recordAuricle = $("#earBugles").val();
            $.ajax({
                url: 'http://localhost:5269/api/SheepArchive/SheepEarBuglesBackFill',
                type: 'post',
                data: JSON.stringify({ recordAuricle: aa }),
                dataType: 'json',
                contentType: 'application/json',
                success: res => {
                    $(res.data).each(function () {
                        $("#sex").val(this.sex)
                        $("#cottageName").val(this.cottageName)
                        $("#fieldName").val(this.fieldName)
                    })
                    console.log(res, 88888)
                }
            })
        }

    })
   
    function Add() {
            var data = {
            crossbreedingTime: $("#crossbreedingTime").val(),
            bredSheepNo: $("#earBugles").val(),
            cottageId: $("#cottageId").val(),
            fieldId: $("#field").val(),
            currentBuildings: $("#cottageName").val(),
            currentColumns: $("#fieldName").val(),
            sex: $("#sex").val(),
            ageTransfer: $("#ageTransfer").val()
            }

            $.ajax({
            url: "http://localhost:5269/api/Crossbreeding/CreateCrossbreeding",
                type: 'post',
                data: JSON.stringify(data),
                dataType: "json",
                contentType: "application/json",
                success: res => {
                    if (res.code == 0) {
                        layer.msg('添加成功')
                        location.href = "/Birth/Descendent/"
                    }
                    else {
                        layer.msg('保存失败')
                    }
                }
            })
        }
</script>